package com.xms.ribbonbutton.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.xms.datacore.service.DeleterService;
import com.xms.dependency.XmsDependencyException;
import com.xms.dependency.service.IDependencyChecker;
import com.xms.event.core.AuthorizationStateChangedEvent;
import com.xms.event.core.IEventPublisher;
import com.xms.ribbonbutton.core.RibbonButtonDefaults;
import com.xms.ribbonbutton.entity.RibbonButton;
import com.xms.utils.CollectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.sql.SQLException;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * RibbonButtonDeleterService
 * </p>
 *
 * @author migo
 * @since 2021/9/8
 */
@CacheConfig(cacheNames = "ribbonbutton", keyGenerator = "CacheKeyGenerator")
@Service
public class RibbonButtonDeleterService extends DeleterService<RibbonButton> implements IRibbonButtonDeleterService {
    private IRibbonButtonDependency _ribbonButtonDependency;
    private IDependencyChecker _dependencyChecker;
    private IEventPublisher _eventPublisher;

    @Autowired
    public RibbonButtonDeleterService(IRibbonButtonDependency ribbonButtonDependency,
                                      IDependencyChecker dependencyChecker,
                                      IEventPublisher eventPublisher) {
        _ribbonButtonDependency = ribbonButtonDependency;
        _dependencyChecker = dependencyChecker;
        _eventPublisher = eventPublisher;
    }

    @CacheEvict(allEntries = true, condition = "#result == true")
    @Transactional(rollbackFor=Exception.class)
    @Override
    public boolean deleteById(UUID id) throws SQLException, XmsDependencyException {
        //检查依赖项
        _dependencyChecker.checkAndThrow(RibbonButtonDefaults.MODULE_NAME, id);
        boolean success = super.removeById(id);
        if (success) {
            //删除依赖项
            _ribbonButtonDependency.delete(id);
            //权限更改事件
            _eventPublisher.publish(new AuthorizationStateChangedEvent(RibbonButtonDefaults.MODULE_NAME, Collections.singletonList(id), false));
        }
        return success;
    }

    @CacheEvict(allEntries = true, condition = "#result == true")
    @Transactional(rollbackFor=Exception.class)
    @Override
    public boolean deleteMany(Wrapper<RibbonButton> queryWrapper) throws SQLException, XmsDependencyException {
        List<RibbonButton> deleteds = super.list(queryWrapper);
        if (CollectionUtil.notEmpty(deleteds)) {
            for (RibbonButton entity : deleteds) {
                this.deleteById(entity.getRibbonbuttonid());
            }
            return true;
        }
        return false;
    }

    @CacheEvict(allEntries = true, condition = "#result == true")
    @Transactional(rollbackFor=Exception.class)
    @Override
    public boolean deleteByIds(Collection<UUID> idList) throws SQLException, XmsDependencyException {
        Assert.notEmpty(idList, "id must not be empty");
        for (UUID id : idList) {
            this.deleteById(id);
        }
        return true;
    }
}
